pacman::p_load(foreign, dplyr, tidyr, tibble, sf,xtable,reshape,openxlsx,questionr)

setwd("")#set working directory

#function to extract the min and max # of precincts correlated
precinct.num <- function(df) {
  vec1<-c()
  vec2<-c()
  frac <- df%>%
    dplyr::select(is.numeric)
  for (i in 1:ncol(frac)){
    frac.temp<-frac[,i]%>%
      na.omit(.)
    vec2<-append(length(unlist(frac.temp[,1])),vec2)
  }
  for (i in 1:(ncol(frac)-1)){
    for (j in (i+1):ncol(frac)){
      vec1<-append((sum(complete.cases(frac[,i], frac[,j]))),vec1)
      print(sum(complete.cases(frac[,i], frac[,j])))
    }
  }
  return(list(max.complete=max(vec1),
              min.complete=min(vec1),
              min.obs=min(vec2),
              max.obs=max(vec2)))
}

#2016 GOP (Pres.) ----- 
cl16 <- function(path,countyname,key){
  df <- read.csv(path)
  df.t <- data.frame(t(df))
  colnames(df.t) <- df[,c(key)]
  df.t$precincts <- colnames(df)
  df.t$precincts[1] <- "precincts"
  if (sum(colnames(df.t)!=df.t[1,])==0){
    df.t <- df.t%>%slice(-1)
  }
  rownames(df.t) <- NULL
  df.t$county <- countyname
  df.t <- df.t %>% 
    dplyr::select(precincts, county, everything())%>%
    filter(!apply(dplyr::select(., (which(names(.) == "county") + 1):ncol(.)), 1, function(row) all(is.na(row))))
  return(df.t)
}

belknap <- cl16("GOP/nh_belknap_2016_clean.csv", "belknap", "X")
carroll <- cl16("GOP/nh_carroll_2016_clean.csv", "carroll", "X")
cheshire <- cl16("GOP/nh_cheshire_2016_clean.csv", "cheshire", "CHESHIRE.COUNTY")
coos <- cl16("GOP/nh_coos_2016_clean.csv", "coos", "COOS.COUNTY")
grafton <- cl16("GOP/nh_grafton_2016_clean.csv", "grafton", "GRAFTON.COUNTY")
hillsborough <- cl16("GOP/nh_hillsborough_2016_clean.csv", "hillsborough", "HILLSBOROUGH.COUNTY")
merrimack <- cl16("GOP/nh_merrimack_2016_clean.csv", "merrimack", "MERRIMACK.COUNTY")
rockingham <- cl16("GOP/nh_rockingham_2016_clean.csv", "rockingham", "ROCKINGHAM.COUNTY")
strafford <- cl16("GOP/nh_strafford_2016_clean.csv", "strafford", "STRAFFORD.COUNTY")
sullivan <- cl16("GOP/nh_sullivan_2016_clean.csv", "sullivan", "SULLIVAN.COUNTY")

pres.gop.16 <- rbind(carroll, belknap, cheshire,coos,grafton,hillsborough,merrimack,rockingham,
                  strafford, sullivan) %>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))

pres.gop.16.prec.voteshares <- pres.gop.16 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(bush.pct.16 = `Bush, r` / totalvotes) %>%
  mutate(trump.pct.16 = `Trump, r` / totalvotes)%>%
  mutate(cruz.pct.16 = `Cruz, r` / totalvotes)%>%
  mutate(kasich.pct.16 = `Kasich, r` / totalvotes) %>%
  mutate(rubio.pct.16 = `Rubio, r` / totalvotes)%>%
  dplyr::select(county, precincts, bush.pct.16,trump.pct.16,cruz.pct.16,kasich.pct.16,rubio.pct.16)  %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  #clean precinct names
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) 

#total voteshares
pres.gop.16.tota.voteshares <-  pres.gop.16 %>%
  dplyr::select(-c("county", "precincts"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2016)
rownames(pres.gop.16.tota.voteshares)<-NULL

#2016 Dems (Pres.) -----
belknap <- cl16("DEMS/nh_belknap_2016_clean .csv", "belknap", "BELKNAP.COUNTY")
carroll <- cl16("DEMS/nh_carroll_2016_clean.csv", "carroll", "CARROLL.COUNTY")
cheshire <- cl16("DEMS/nh_cheshire_2016_clean.csv", "cheshire", "CHESHIRE.COUNTY")
coos <- cl16("DEMS/nh_coos_2016_clean.csv", "coos", "COOS.COUNTY")
grafton <- cl16("DEMS/nh_grafton_2016_clean.csv", "grafton", "GRAFTON.COUNTY")
hillsborough <- cl16("DEMS/nh_hillsborough_2016_clean.csv", "hillsborough", "HILLSBOROUGH.COUNTY")
merrimack <- cl16("DEMS/nh_merrimack_2016_clean.csv", "merrimack", "MERRIMACK.COUNTY")
rockingham <- cl16("DEMS/nh_rockingham_2016_clean.csv", "rockingham", "ROCKINGHAM.COUNTY")
strafford <- cl16("DEMS/nh_strafford_2016_clean.csv", "strafford", "STRAFFORD.COUNTY")
sullivan <- cl16("DEMS/nh_sullivan_2016_clean.csv", "sullivan", "SULLIVAN.COUNTY")

pres.dems.16 <- rbind(carroll, belknap, cheshire,coos,grafton,hillsborough,merrimack,rockingham,
                     strafford, sullivan) %>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))

pres.dems.16.prec.voteshares <- pres.dems.16 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(sanders.pct.16 = `Sanders, d` / totalvotes) %>%
  mutate(clinton.pct.16 = `Clinton, d` / totalvotes)%>%
  dplyr::select(county, precincts, sanders.pct.16,clinton.pct.16) %>%
  #clean precinct names
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) 
  
#total voteshares
pres.dems.16.tota.voteshares <-  pres.dems.16 %>%
  dplyr::select(-c("county", "precincts"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares)  %>%
  mutate(year=2016)
rownames(pres.dems.16.tota.voteshares)<-NULL

#correcting a precinct name
pres.dems.16.prec.voteshares$precincts <- pres.dems.16.prec.voteshares$precincts %>%
  dplyr::recode(
    "fitzilliam" = "fitzwilliam"
  )  

#2022 GOP (Senate) ------ 
sen.gop.22 <- read.xlsx("nh_gop_senate_primary_2022.xlsx")%>% 
  mutate(county=tolower(county))%>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&']", "", precincts))) %>% 
  mutate(precincts = tolower(gsub("academy", "ac", precincts))) %>%
  dplyr::select(precincts, county, everything()) %>%
  dplyr::select(-c(Scatter, `John.Riggieri,.d`, `Maggie.Hassan,.d`, `Paul.J..Krautmann,.d`))#removing non-Republicans

#correcting a precinct name
sen.gop.22$precincts <- sen.gop.22$precincts %>%
  dplyr::recode(
    "thompsonmespur" = "thompsonmesspur"
   )  

#NOTE: Inferring that missing precinct label is new hampton
sen.gop.22$precincts[14] <- "newhampton"

sen.gop.22 <- sen.gop.22 %>%
  mutate(across((which(names(.) == "party") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  dplyr::select(-party)

sen.gop.22.prec.voteshares <- sen.gop.22 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(bolduc.pct.22 = `Donald.C..Bolduc,.r` / totalvotes) %>%
  mutate(smith.pct.22 = `Kevin.Smith,.r` / totalvotes) %>% 
  mutate(morse.pct.22 = `Chuck.Morse,.r` / totalvotes) %>%
  dplyr::select(precincts, county, bolduc.pct.22, smith.pct.22, morse.pct.22)

sen.gop.22.tota.voteshares <-  sen.gop.22 %>%
  dplyr::select(-c("county", "precincts"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2022)

#2008 Dems (Pres.)-----
pres.dems.08 <- read.csv('NH_DemPrimaries_2008.csv')%>%
  subset(., !grepl("TOTAL", precincts, ignore.case=T))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&']", "", precincts))) %>% 
  mutate(precincts = tolower(gsub("academy", "ac", precincts))) %>% 
  mutate(precincts = tolower(gsub("atkgilmantonacgt", "atgilacgt", precincts))) %>%#just changing from Gilmanton to Gil. for consistency
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))

pres.dems.08.prec.voteshares <- pres.dems.08 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(edwards.pct.08 = `Edwards..d` / totalvotes) %>%
  mutate(obama.pct.08 = `Obama..d` / totalvotes) %>% 
  mutate(clinton.pct.08 = `Clinton..d` / totalvotes)%>%
  dplyr::select(county, precincts, edwards.pct.08, obama.pct.08, clinton.pct.08)

pres.dems.08.tota.voteshares <-  pres.dems.08 %>%
  dplyr::select(contains("..d"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares)  %>%
  mutate(year=2008)

#2008 GOP (Pres.)------
pres.gop.08 <- read.csv("NH_GOPPrimary_2008.csv")%>%
  dplyr::select(precincts, county, everything())%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  subset(., !grepl("TOTAL", precincts, ignore.case=T))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&']", "", precincts))) %>% 
  mutate(precincts = tolower(gsub("academy", "ac", precincts))) %>% 
  mutate(precincts = tolower(gsub("atkgilmantonacgt", "atgilacgt", precincts)))#just changing from Gilmanton to Gil. for consistency

pres.gop.08.prec.voteshares <- pres.gop.08 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(mccain.pct.08 = `McCain..r` / totalvotes) %>%
  mutate(huckabee.pct.08 = `Huckabee..r` / totalvotes) %>% 
  mutate(romney.pct.08 = `Romney..r` / totalvotes)%>%
  dplyr::select(county, precincts, romney.pct.08, huckabee.pct.08, mccain.pct.08)

pres.gop.08.tota.voteshares <-  pres.gop.08 %>%
  dplyr::select(contains("..r"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2008)

#2012 GOP (Pres) ------ 
pres.gop.12 <- read.xlsx("nh_gop_2012.xlsx")%>%
  dplyr::select(precincts, county, everything())%>%
  subset(., !grepl("TOTAL", precincts, ignore.case=T))%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("-", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&']", "", precincts))) %>% 
  mutate(precincts = tolower(gsub("academy", "ac", precincts))) %>% 
  mutate(precincts = tolower(gsub("wd1", "ward1", precincts))) %>% 
  mutate(precincts = tolower(gsub("wd2", "ward2", precincts))) %>% 
  mutate(precincts = tolower(gsub("wd3", "ward3", precincts))) %>% 
  mutate(precincts = tolower(gsub("wd4", "ward4", precincts))) %>% 
  mutate(precincts = tolower(gsub("w5", "ward5", precincts))) %>% 
  mutate(precincts = tolower(gsub("atkgilmantonacgt", "atgilacgt", precincts)))%>%#just changing from Gilmanton to Gil. for consistency
  mutate(precincts = tolower(gsub("lemspter", "lempster", precincts)))%>%#Appears to be a mispelling
  mutate(precincts=ifelse(precincts=="ward1", "claremontward1",precincts))%>%#appears to be an omission in the labels
  mutate(precincts=ifelse(precincts=="ward2", "claremontward2",precincts))%>%#appears to be an omission in the labels
  mutate(precincts=ifelse(precincts=="ward3", "claremontward3",precincts))#appears to be an omission in the labels

pres.gop.12.prec.voteshares <- pres.gop.12 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(santorum.pct.12 =Santorum_GOP12_PRESIDENT / totalvotes) %>%
  mutate(paul.pct.12 =Paul_GOP12_PRESIDENT / totalvotes) %>%
  mutate(gingrich.pct.12 =Gingrich_GOP12_PRESIDENT / totalvotes) %>%
  mutate(romney.pct.12 =Romney_GOP12_PRESIDENT / totalvotes) %>%
  mutate(huntsman.pct.12 =Huntsman_GOP12_PRESIDENT / totalvotes) %>%
  dplyr::select(county, precincts, huntsman.pct.12, romney.pct.12, gingrich.pct.12, paul.pct.12,santorum.pct.12)

pres.gop.12.tota.voteshares <-  pres.gop.12 %>%
  dplyr::select(contains("GOP12_PRESIDENT"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2012)

#2024 GOP (Pres) ------ 
pres.gop.24 <- read.csv("nh_gop_2024_precincts.csv")%>%
  subset(., !grepl("total", precincts, ignore.case=T))%>%
  dplyr::select(precincts, county, everything())%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("-", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&'*]", "", precincts))) %>% 
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>%
  mutate(precincts = tolower(gsub("atgilacademygt", "atgilacgt", precincts))) %>%#for consistency 
  mutate(precincts = tolower(gsub("keeneware5", "keeneward5", precincts))) %>%#mispelling
  mutate(precincts = tolower(gsub("thompsonmespur", "thompsonmesspur", precincts))) #mispelling

pres.gop.24.prec.voteshares <- pres.gop.24 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(trump.pct.24 =TRUMP_GOP24_COUNT / totalvotes) %>%
  mutate(haley.pct.24 =HALEY_GOP24_COUNT / totalvotes) %>%
  dplyr::select(county, precincts, haley.pct.24, trump.pct.24)

pres.gop.24.tota.voteshares <-  pres.gop.24 %>%
  dplyr::select(contains("GOP24_COUNT"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2024)

#2010 GOP (Senate)------ 
sen.gop.10 <- read.csv("NH_GOPPrimaries_Senate_2010.csv")%>%
  subset(., !grepl("total", precincts, ignore.case=T))%>%
  dplyr::select(precincts, county, everything())%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("-", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&'*]", "", precincts)))%>%
  mutate(precincts = tolower(gsub("grant", "gt", precincts))) %>%
  mutate(precincts = tolower(gsub("atgilacademygt", "atgilacgt", precincts))) %>%#for consistency 
  mutate(precincts = tolower(gsub("newprot", "newport", precincts)))#mispelling

sen.gop.10.prec.voteshares <- sen.gop.10 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(ayotte.pct.10 =Kelly.Ayotte..r / totalvotes) %>%
  mutate(binnie.pct.10 =Bill.Binnie..r / totalvotes) %>%
  mutate(lamontagne.pct.10 =Ovide.Lamontagne..r / totalvotes) %>%
  dplyr::select(county, precincts, ayotte.pct.10, binnie.pct.10, lamontagne.pct.10)

colnames(sen.gop.10)
sen.gop.10.tota.voteshares <-  sen.gop.10 %>%
  dplyr::select(contains("..r"))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2010)

#2020 GOP (Senate)------
sen.gop.20 <- read.xlsx("gop2020_senate_raw.xlsx")%>%
  subset(., !grepl("total", precincts, ignore.case=T))%>%
  dplyr::select(precincts, county, everything())%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("-", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&'*]", "", precincts)))%>%
  mutate(precincts = tolower(gsub("grant", "gt", precincts)))%>%
  mutate(precincts = tolower(gsub("atgilacademygt", "atgilacgt", precincts))) %>%#for consistency 
  mutate(precincts = tolower(gsub("thompsonmespur", "thompsonmesspur", precincts))) #mispelling

sen.gop.20.prec.voteshares <- sen.gop.20 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(bolduc.pct.20 =BOLDUC / totalvotes) %>%
  mutate(messner.pct.20 =MESSNER / totalvotes) %>%
  dplyr::select(county, precincts, bolduc.pct.20, messner.pct.20)

sen.gop.20.tota.voteshares<-sen.gop.20 %>%
  dplyr::select(-c(precincts, county))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2020)

#2020 Dems (Pres.)--------
pres.dems.20 <- read.xlsx("dem_primary_2020_raw.xlsx")%>%
  subset(., !grepl("total", precincts, ignore.case=T))%>%
  dplyr::select(precincts, county, everything())%>%
  mutate(across((which(names(.) == "county") + 1):ncol(.), ~ replace_na(as.numeric(.), 0)))%>%
  #consolidate Berlin wards for consistency with following years
  mutate(precincts = ifelse(grepl("berlin", precincts, ignore.case = TRUE), "berlin", precincts)) %>%
  group_by(precincts, county)%>%
  summarise(across(everything(), sum, na.rm = TRUE))%>%
  ungroup() %>%
  #clean precinct names
  mutate(precincts = tolower(gsub(" ", "", precincts))) %>%
  mutate(precincts = tolower(gsub("\\.", "", precincts))) %>%
  mutate(precincts = tolower(gsub("-", "", precincts))) %>%
  mutate(precincts = tolower(gsub("location", "loc", precincts))) %>%
  mutate(precincts = tolower(gsub("purchase", "pur", precincts))) %>% 
  mutate(precincts = tolower(gsub("college", "coll", precincts))) %>% 
  mutate(precincts = tolower(gsub("[&'*]", "", precincts)))%>%
  mutate(precincts = tolower(gsub("grant", "gt", precincts)))%>%
  mutate(precincts = tolower(gsub("atgilacademygt", "atgilacgt", precincts))) %>%#for consistency 
  mutate(precincts = tolower(gsub("thompsonmespur", "thompsonmesspur", precincts)))%>%#mispelling
  mutate(precincts = tolower(gsub("fitzilliam", "fitzwilliam", precincts))) #mispelling

pres.dems.20.prec.voteshares <- pres.dems.20 %>%
  mutate(totalvotes = rowSums(across((which(names(.) == "county") + 1):ncol(.)))) %>%
  mutate(sanders.pct.20 =`Sanders,.d` / totalvotes) %>%
  mutate(klobuchar.pct.20 =`Klobuchar,.d` / totalvotes) %>%
  mutate(buttgieg.pct.20 =`Buttigieg,.d` / totalvotes) %>%
  dplyr::select(county,precincts, klobuchar.pct.20, sanders.pct.20, buttgieg.pct.20)

pres.dems.20.tota.voteshares <-  pres.dems.20 %>%
  dplyr::select(-c(precincts, county))%>%
  colSums(.) %>%
  as.data.frame(.) %>%
  setNames("voteshares") %>%
  mutate(cand = rownames(.)) %>%
  mutate(voteshares = voteshares / sum(voteshares))%>%
  dplyr::select(cand, voteshares) %>%
  mutate(year=2020)

#Merge precinct fractions -----
merged.gop<-pres.gop.08.prec.voteshares%>%
  dplyr::select(-county)%>%
  full_join(sen.gop.10.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))%>%
  full_join(pres.gop.12.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))%>%
  full_join(pres.gop.16.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))%>%
  full_join(sen.gop.20.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))%>%
  full_join(sen.gop.22.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))%>%
  full_join(pres.gop.24.prec.voteshares%>%
              dplyr::select(-county),by=c("precincts"))

#load NH shapefile
shapename.vd <- read_sf('')
freq(shapename.vd$NAME)%>%
  filter(n>1)#doesn't look like the names encompass ward div 

shapename.vd$NAME[shapename.vd$NAME=="Bean's Purchase"] <- "BeansPur"
shapename.vd$NAME[shapename.vd$NAME=="Bean's Grant"] <- "BeansGt"
shapename.vd$NAME[shapename.vd$NAME=="Waterville Valley"] <- "WatervilleValley"
shapename.vd$NAME[shapename.vd$NAME=="Center Harbor"] <- "CenterHarbor"
shapename.vd$NAME[shapename.vd$NAME=="New Hampton"] <- "NewHampton"
shapename.vd$NAME[shapename.vd$NAME=="New Durham"] <- "NewDurham"
shapename.vd$NAME[shapename.vd$NAME=="New Boston"] <- "NewBoston"
shapename.vd$NAME[shapename.vd$NAME=="New London"] <- "NewLondon"
shapename.vd$NAME[shapename.vd$NAME=="Bean's Purchase"] <- "BeansPur"
shapename.vd$NAME[shapename.vd$NAME=="Chandler's Purchase"] <- "ChandlersPurchase"
shapename.vd$NAME[shapename.vd$NAME=="Crawford's Purchase"] <- "CrawfordsPur"
shapename.vd$NAME[shapename.vd$NAME=="Sugar Hill"] <- "SugarHill"
shapename.vd$NAME[shapename.vd$NAME=="South Hampton"] <- "SouthHampton"
shapename.vd$NAME[shapename.vd$NAME=="Mont Vernon"] <- "MontVernon"
shapename.vd$NAME[shapename.vd$NAME=="Fitzwilliam"] <- "Fitzilliam"
shapename.vd$NAME[shapename.vd$NAME=="North Hampton"] <- "NorthHampton"
shapename.vd$NAME[shapename.vd$NAME=="East Kingston"] <- "EastKingston"
shapename.vd$NAME[shapename.vd$NAME=="New Ipswich"] <- "NewIpswich"
shapename.vd$NAME[shapename.vd$NAME=="Hampton Falls"] <- "HamptonFalls"
shapename.vd$NAME[shapename.vd$NAME=="New Castle"] <- "NewCastle"
shapename.vd$NAME[shapename.vd$NAME=="Atkinson & Gilmanton"] <- "AtGilAcGt"
shapename.vd$NAME[shapename.vd$NAME=="Second College Grant"] <- "SecondCollGt"
shapename.vd$NAME[shapename.vd$NAME=="Dix's Grant"] <- "DixsGt"
shapename.vd$NAME[shapename.vd$NAME=="Wentworth's Location"] <- "WentworthsLoc"
shapename.vd$NAME[shapename.vd$NAME=="Erving's Location"] <- "ErvingsLoc"
shapename.vd$NAME[shapename.vd$NAME=="Low & Burbanks Grant"] <- "LowBurbanksGt"
shapename.vd$NAME[shapename.vd$NAME=="Martin's Location"] <- "MartinsLoc"
shapename.vd$NAME[shapename.vd$NAME=="Thompson & Meserve"] <- "ThompsonMessPur"
shapename.vd$NAME[shapename.vd$NAME=="Green's Grant"] <- "GreensGt"
shapename.vd$NAME[shapename.vd$NAME=="Pinkham's Grant"] <- "PinkhamsGt"
shapename.vd$NAME[shapename.vd$NAME=="Sargent's Purchase"] <- "SargentsPur"
shapename.vd$NAME[shapename.vd$NAME=="Cutt's Grant"] <- "CuttsGt"
shapename.vd$NAME[shapename.vd$NAME=="Hart's Location"] <- "HartsLoc"
shapename.vd$NAME[shapename.vd$NAME=="Hadley's Purchase"] <- "HadleysPur"
shapename.vd$NAME[shapename.vd$NAME=="Hale's Location"] <- "HalesLoc"

merged.gop$NAME.WARD <- merged.gop$precincts
merged.gop$NAME.WARD <-gsub("ward11","-11",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward12","-12",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward10","-10",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward1","-1",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward2","-2",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward3","-3",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward4","-4",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward5","-5",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward6","-6",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward7","-7",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward8","-8",as.character(merged.gop$NAME.WARD))
merged.gop$NAME.WARD <-gsub("ward9","-9",as.character(merged.gop$NAME.WARD))

txt <- c()
for(i in 1:dim(shapename.vd)[1]){
  hold <- strsplit(shapename.vd$WARD," ")[[i]]
  n.blocks <- length(hold)
  if(hold[n.blocks]=="Entire"){
    txt[i] <- NA
  }else{
    txt[i] <- as.numeric(hold[n.blocks])
  }
}

shapename.vd$WARDR <- txt
shapename.vd$NAME.WARD <- tolower(paste(shapename.vd$NAME,"-",shapename.vd$WARDR,sep=""))
merged.gop$NAME.WARD<-ifelse(grepl("-", merged.gop$NAME.WARD), merged.gop$NAME.WARD, paste0(merged.gop$NAME.WARD, "-na"))

#clean a few precinct names 
shapename.vd$NAME.WARD<-shapename.vd$NAME.WARD%>%
  dplyr::recode(
    "fitzilliam-na"="fitzwilliam-na", 
    "chandlerspurchase-na"="chandlerspur-na",
    "grantham-na"="gtham-na"
  )

shapename.vd$NAME.WARD[!shapename.vd$NAME.WARD%in%merged.gop$NAME.WARD]
merged.gop$NAME.WARD[!merged.gop$NAME.WARD%in%shapename.vd$NAME.WARD]
#NOTE: Derry dropped because it isn't matched

shapename.merged.gop<-inner_join(shapename.vd,merged.gop,by=c("NAME.WARD"))%>%
  distinct(NAME.WARD,.keep_all = T)#remove one of the somersworth ward 3 obs. 

#merge in total 2024 votes
sum.na <- function(x){
  sum(x,na.rm=T)
}

#prepare for cex item
pres.gop.24$GOP24_TOT <- apply(cbind(pres.gop.24$HALEY_GOP24_COUNT,
                                     pres.gop.24$TRUMP_GOP24_COUNT),1,sum.na)
pres.gop.24$NAME.WARD <- pres.gop.24$precincts
pres.gop.24$NAME.WARD <-gsub("ward11","-11",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward12","-12",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward10","-10",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward1","-1",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward2","-2",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward3","-3",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward4","-4",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward5","-5",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward6","-6",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward7","-7",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward8","-8",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD <-gsub("ward9","-9",as.character(pres.gop.24$NAME.WARD))
pres.gop.24$NAME.WARD<-ifelse(grepl("-", pres.gop.24$NAME.WARD), pres.gop.24$NAME.WARD, paste0(pres.gop.24$NAME.WARD, "-na"))
shapename.merged.gop.tot<-inner_join(shapename.merged.gop,pres.gop.24,by=c("NAME.WARD"))

pdf("figureA10.pdf")
par(pty="s")
plot(shapename.merged.gop.tot$trump.pct.16,
     shapename.merged.gop.tot$trump.pct.24,
     pch=16,
     cex=shapename.merged.gop.tot$GOP24_TOT/2500,
     xlab="Trump Percentage '16",ylab="Trump Percentage '24",ylim=c(0,1),xlim=c(0,1))
abline(a=0,b=1,lty=2)
dev.off()

cormatr.gop <- cor(merged.gop%>%
                     dplyr::select(-c(precincts,NAME.WARD)),use="pairwise.complete.obs")
cormatr.gop2 <- cormatr.gop[,-c(1,2,3)]

precinct.num(merged.gop)

print(xtable(cormatr.gop2, 
             label="f:nh", 
             caption=c("Pearson correlations between Republican candidate vote shares in New Hampshire primaries. N=299-301 precincts."),
             digits=c(0,rep(3,20))),
      scalebox=0.6, 
      file=c("tableA16.tex"))#table A16

tableA16 <- readLines("tableA16.tex")
tableA16 <- gsub("mccain.pct.08", "McCain '08", tableA16)
tableA16 <- gsub("huckabee.pct.08", "Huckabee '08", tableA16)
tableA16 <- gsub("romney.pct.08", "Romney '08", tableA16)
tableA16 <- gsub("morse.pct.22", "Morse '22", tableA16)
tableA16 <- gsub("smith.pct.22", "Smith '22", tableA16)
tableA16 <- gsub("bolduc.pct.22", "Bolduc '22", tableA16)
tableA16 <- gsub("trump.pct.24", "Trump '24", tableA16)
tableA16 <- gsub("haley.pct.24", "Haley '24", tableA16)
tableA16 <- gsub("rubio.pct.16", "Rubio '16", tableA16)
tableA16 <- gsub("kasich.pct.16", "Kasich '16", tableA16)
tableA16 <- gsub("cruz.pct.16", "Cruz '16", tableA16)
tableA16 <- gsub("trump.pct.16", "Trump '16", tableA16)
tableA16 <- gsub("bush.pct.16", "Bush '16", tableA16)
tableA16 <- gsub("santorum.pct.12", "Santorum '12", tableA16)
tableA16 <- gsub("paul.pct.12", "Paul '12", tableA16)
tableA16 <- gsub("gingrich.pct.12", "Gingrich '12", tableA16)
tableA16 <- gsub("romney.pct.12", "Romney '12", tableA16)
tableA16 <- gsub("huntsman.pct.12", "Huntsman '12", tableA16)
tableA16 <- gsub("messner.pct.20", "Messner '20", tableA16)
tableA16 <- gsub("bolduc.pct.20", "Bolduc '20", tableA16)
tableA16 <- gsub("lamontagne.pct.10", "Lamontagne '10", tableA16)
tableA16 <- gsub("binnie.pct.10", "Binnie '10", tableA16)
tableA16 <- gsub("ayotte.pct.10", "Ayotte '10", tableA16)
tableA16 <- append(tableA16, "\\begin{scriptsize}", after = 5)
tableA16 <- append(tableA16, "\\end{scriptsize}", after = 35)
writeLines(tableA16, "tableA16.tex")

merged.dems<-pres.dems.08.prec.voteshares%>%
  full_join(pres.dems.16.prec.voteshares,by=c("county", "precincts"))%>%
  full_join(pres.dems.20.prec.voteshares,by=c("county", "precincts"))%>%
  dplyr::select(-county)

cormatr.dems <- cor(merged.dems%>%
                      dplyr::select(-c(precincts)),use="pairwise.complete.obs")
cormatr.dems2 <- cormatr.dems[,-c(1,2,3)]

precinct.num(merged.dems)

print(xtable(cormatr.dems2, 
             label="f:nhdems", 
             caption=c("Correlations of vote shares in Democratic primaries from New Hampshire precincts (n=300-302)."),
             digits=c(0,rep(3,5))),
      file=c("tableA17.tex"))#table A17

tableA17 <- readLines("tableA17.tex")
tableA17 <- gsub("edwards.pct.08", "Edwards '08", tableA17)
tableA17 <- gsub("obama.pct.08", "Obama '08", tableA17)
tableA17 <- gsub("clinton.pct.08", "Clinton '08", tableA17)
tableA17 <- gsub("sanders.pct.16", "Sanders '16", tableA17)
tableA17 <- gsub("clinton.pct.16", "Clinton '16", tableA17)
tableA17 <- gsub("klobuchar.pct.20", "Klobuchar '20", tableA17)
tableA17 <- gsub("sanders.pct.20", "Sanders '20", tableA17)
tableA17 <- gsub("buttgieg.pct.20", "Buttgieg '20", tableA17)
writeLines(tableA17, "tableA17.tex")

#correlation table for regression analysis------ 

#dems
cormatd3 <- data.frame(cbind(variable_2 = rownames(cormatr.dems2), cormatr.dems2))
cormatd4 <- melt(cormatd3, id = c("variable_2"))

cormatd4$year_cand1_0 <- ifelse(grepl("pct.08", cormatd4$variable_2), 2008, 0)
cormatd4$year_cand1_1 <- ifelse(grepl("pct.10", cormatd4$variable_2), 2010, 0)
cormatd4$year_cand1_2 <- ifelse(grepl("pct.12", cormatd4$variable_2), 2012, 0)
cormatd4$year_cand1_3 <- ifelse(grepl("pct.16", cormatd4$variable_2), 2016, 0)
cormatd4$year_cand1_4 <- ifelse(grepl("pct.18", cormatd4$variable_2), 2018, 0)
cormatd4$year_cand1_5 <- ifelse(grepl("pct.20", cormatd4$variable_2), 2020, 0)
cormatd4$year_cand1_6 <- ifelse(grepl("pct.22", cormatd4$variable_2), 2022, 0)
cormatd4$year_cand1_7 <- ifelse(grepl("pct.24", cormatd4$variable_2), 2022, 0)
cormatd4$year_cand1 <- cormatd4$year_cand1_1 + cormatd4$year_cand1_2 + cormatd4$year_cand1_3 + cormatd4$year_cand1_4 + cormatd4$year_cand1_0 + cormatd4$year_cand1_5 + cormatd4$year_cand1_6+ cormatd4$year_cand1_7
cormatd4$year_cand2_0 <- ifelse(grepl("pct.08", cormatd4$variable), 2008, 0)
cormatd4$year_cand2_1 <- ifelse(grepl("pct.10", cormatd4$variable), 2010, 0)
cormatd4$year_cand2_2 <- ifelse(grepl("pct.12", cormatd4$variable), 2012, 0)
cormatd4$year_cand2_3 <- ifelse(grepl("pct.16", cormatd4$variable), 2016, 0)
cormatd4$year_cand2_4 <- ifelse(grepl("pct.18", cormatd4$variable), 2018, 0)
cormatd4$year_cand2_5 <- ifelse(grepl("pct.20", cormatd4$variable), 2020, 0)
cormatd4$year_cand2_6 <- ifelse(grepl("pct.22", cormatd4$variable), 2022, 0)
cormatd4$year_cand2_7 <- ifelse(grepl("pct.24", cormatd4$variable), 2024, 0)

cormatd4$year_cand2 <- cormatd4$year_cand2_1 + cormatd4$year_cand2_2 + cormatd4$year_cand2_3 + cormatd4$year_cand2_4 + cormatd4$year_cand2_0 + cormatd4$year_cand2_5 + cormatd4$year_cand2_6+cormatd4$year_cand2_7
cormatd5 <-cormatd4 %>%
  subset(., select=c(variable_2, year_cand1, variable, year_cand2, value))

cormatd5$value <- as.numeric(cormatd5$value)
cormatd5$variable <- as.character(cormatd5$variable)
cormatd5$party <- "DEM"
cormatd5$state <- 'NH'

dems.grouped<-cormatd5%>%
  filter(year_cand1!=year_cand2)%>%
  filter(variable_2!=variable)%>%
  group_by(party,year_cand1,year_cand2)%>%
  dplyr::summarise(cor_mean=mean(value,na.rm=T), 
                   cor_median=median(value,na.rm=T))

write.csv(cormatd5, "nh_dems_groupbyyear_corrs.csv")

#gop
cormatd3 <- data.frame(cbind(variable_2 = rownames(cormatr.gop2), cormatr.gop2))
cormatd4 <- melt(cormatd3, id = c("variable_2"))

cormatd4$year_cand1_0 <- ifelse(grepl("pct.08", cormatd4$variable_2), 2008, 0)
cormatd4$year_cand1_1 <- ifelse(grepl("pct.10", cormatd4$variable_2), 2010, 0)
cormatd4$year_cand1_2 <- ifelse(grepl("pct.12", cormatd4$variable_2), 2012, 0)
cormatd4$year_cand1_3 <- ifelse(grepl("pct.16", cormatd4$variable_2), 2016, 0)
cormatd4$year_cand1_4 <- ifelse(grepl("pct.18", cormatd4$variable_2), 2018, 0)
cormatd4$year_cand1_5 <- ifelse(grepl("pct.20", cormatd4$variable_2), 2020, 0)
cormatd4$year_cand1_6 <- ifelse(grepl("pct.22", cormatd4$variable_2), 2022, 0)
cormatd4$year_cand1_7 <- ifelse(grepl("pct.24", cormatd4$variable_2), 2024, 0)
cormatd4$year_cand1 <- cormatd4$year_cand1_1 + cormatd4$year_cand1_2 + cormatd4$year_cand1_3 + cormatd4$year_cand1_4 + cormatd4$year_cand1_0 + cormatd4$year_cand1_5 + cormatd4$year_cand1_6+ cormatd4$year_cand1_7
cormatd4$year_cand2_0 <- ifelse(grepl("pct.08", cormatd4$variable), 2008, 0)
cormatd4$year_cand2_1 <- ifelse(grepl("pct.10", cormatd4$variable), 2010, 0)
cormatd4$year_cand2_2 <- ifelse(grepl("pct.12", cormatd4$variable), 2012, 0)
cormatd4$year_cand2_3 <- ifelse(grepl("pct.16", cormatd4$variable), 2016, 0)
cormatd4$year_cand2_4 <- ifelse(grepl("pct.18", cormatd4$variable), 2018, 0)
cormatd4$year_cand2_5 <- ifelse(grepl("pct.20", cormatd4$variable), 2020, 0)
cormatd4$year_cand2_6 <- ifelse(grepl("pct.22", cormatd4$variable), 2022, 0)
cormatd4$year_cand2_7 <- ifelse(grepl("pct.24", cormatd4$variable), 2024, 0)

cormatd4$year_cand2 <- cormatd4$year_cand2_1 + cormatd4$year_cand2_2 + cormatd4$year_cand2_3 + cormatd4$year_cand2_4 + cormatd4$year_cand2_0 + cormatd4$year_cand2_5 + cormatd4$year_cand2_6+cormatd4$year_cand2_7
cormatd5 <-cormatd4 %>%
  subset(., select=c(variable_2, year_cand1, variable, year_cand2, value))

cormatd5$value <- as.numeric(cormatd5$value)
cormatd5$variable <- as.character(cormatd5$variable)
cormatd5$party <- "GOP"
cormatd5$state <- 'NH'

gop.grouped<-cormatd5%>%
  filter(year_cand1!=year_cand2)%>%
  filter(variable_2!=variable)%>%
  group_by(party,year_cand1,year_cand2)%>%
  dplyr::summarise(cor_mean=mean(value,na.rm=T), 
                   cor_median=median(value,na.rm=T))

write.csv(cormatd5, "nh_gop_groupbyyear_corrs.csv")

# Merge voteshares
merged.voteshares <- rbind(pres.gop.08.tota.voteshares, pres.gop.12.tota.voteshares, pres.gop.16.tota.voteshares, pres.gop.24.tota.voteshares,
                           pres.dems.08.tota.voteshares, pres.dems.16.tota.voteshares, pres.dems.20.tota.voteshares,
                           sen.gop.10.tota.voteshares, sen.gop.20.tota.voteshares, sen.gop.22.tota.voteshares)
write.csv(merged.voteshares, "nh-voteshares.csv")


#prepare corr summary table
corr.summary<-rbind(dems.grouped, gop.grouped)%>%
  setNames(c("Party","Year 1", "Year 2", "Corr. Mean", "Corr. Median"))%>%
  mutate(Party=Party%>%dplyr::recode(
    "DEM"="Democrats", 
    "GOP"="Republicans"
  ))

